package org.ohuyo.libra.server.dao.impl;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.ohuyo.commons.query.HibernateDaoSupportEx;
import org.ohuyo.libra.server.dao.AttributeDao;
import org.ohuyo.libra.server.model.table.AttributeRecord;
import org.ohuyo.libra.server.model.table.AttributeSubRecord;
import org.springframework.stereotype.Service;

/**
 * 
 * @author rabbit
 * @since 2011-9-7
 */
@Service("org.ohuyo.libra.server.attributeDao")
public class AttributeDaoImpl extends HibernateDaoSupportEx implements
		AttributeDao {
	@Resource(name = "org.ohuyo.libra.server.sessionFactory")
	public void setSessionFactoryEx(SessionFactory sessionFactory) {
		super.setSessionFactoryEx(sessionFactory);
	}

	public void saveAttributeRecord(AttributeRecord sessionRecord) {
		getHibernateTemplate().save(sessionRecord);
	}

	public void updateAttributeRecord(AttributeRecord sessionRecord) {
		getHibernateTemplate().update(sessionRecord);

	}

	public AttributeRecord getAttributeRecord(long loginTicket, String key,
			String srcAppCode, String targetAppCode, boolean shared,
			boolean disposable) {
		if (shared) {
			String sql = "from AttributeRecord where loginTicket=? and keyName=? and srcAppCode=? and targetAppCode is null and shared=? and disposable=?";
			logger.debug("===============================loginTicket:"+loginTicket+";=====keyName:"+key+";=======srcAppCode:"+srcAppCode+";===shared:"+shared+";==disposable:"+disposable);
			return (AttributeRecord) this.getHibernateTemplateEx().getByHql(
					sql, loginTicket, key, srcAppCode, shared, disposable);
		} else {
			String sql = "from AttributeRecord where loginTicket=? and keyName=? and srcAppCode = ? and targetAppCode = ? and shared=? and disposable=?";
			return (AttributeRecord) this.getHibernateTemplateEx().getByHql(
					sql, loginTicket, key, srcAppCode, targetAppCode, shared,
					disposable);
		}
	}

	public int deleteAttributeRecord(long loginTicket, String key,
			String appCode) {
		return this
				.getHibernateTemplateEx()
				.executeUpdateBySql(
						"delete from t_attribute where login_ticket=? and keyName=? and src_app_code=? and target_app_code=?",
						loginTicket, key, appCode, appCode);
	}

	public int deleteAttributeRecordByLoginTicket(long loginTicket) {
		return this.getHibernateTemplateEx().executeUpdateBySql(
				"delete from t_attribute where login_ticket=?", loginTicket);
	}

	public void deleteAttributeRecord(AttributeRecord attributeRecord) {
		this.getHibernateTemplate().delete(attributeRecord);
	}

	public AttributeRecord getAttributeRecordWhithLock(long loginTicket,
			String key, String srcAppCode, String targetAppCode,
			boolean shared, boolean disposable) {
		String update = "update AttributeRecord set loginTicket = loginTicket ";
		String from = "from AttributeRecord ";
		if (shared) {
			String where = " where loginTicket=? and keyName=? and srcAppCode=? and targetAppCode is null and shared=? and disposable=? ";
			int i = this.getHibernateTemplateEx().executeUpdateByHql(
					update + where, loginTicket, key, srcAppCode, shared,
					disposable);
			if (i == 0) {
				return null;
			}
			String sql = from + where;
			return (AttributeRecord) this.getHibernateTemplateEx().getByHql(
					sql, loginTicket, key, srcAppCode, shared, disposable);
		} else {
			String where = " where loginTicket=? and keyName=? and srcAppCode = ? and targetAppCode = ? and shared=? and disposable=? ";
			int i = this.getHibernateTemplateEx().executeUpdateByHql(
					update + where, loginTicket, key, srcAppCode,
					targetAppCode, shared, disposable);
			if (i == 0) {
				return null;
			}
			String sql = from + where;
			return (AttributeRecord) this.getHibernateTemplateEx().getByHql(
					sql, loginTicket, key, srcAppCode, targetAppCode, shared,
					disposable);
		}

	}

	public int deleteInvalidAttributeRecord() {
		return this.getHibernateTemplateEx().executeUpdateBySql(
				"delete from t_attribute where login_ticket not in "
						+ "(select a.login_ticket from t_login_ticket a)");
	}

	public int deleteInvalidAttributeSubRecord() {
		return this.getHibernateTemplateEx().executeUpdateBySql(
				"delete from t_attribute_sub where i_attribute not in "
						+ "(select a.i_attribute from t_attribute a)");
	}

	// -------------------------------------
	public int deleteAttributeSubByLoginTicket(long loginTicket) {
		return this.getHibernateTemplateEx().executeUpdateBySql(
				"delete from t_attribute_sub where login_ticket = ?",
				loginTicket);
	}

	public void saveAttributeSub(AttributeSubRecord attributeSub) {
		this.getHibernateTemplate().save(attributeSub);
	}

	public void updateAttributeSub(AttributeSubRecord attributeSub) {
		this.getHibernateTemplate().update(attributeSub);
	}

	public AttributeSubRecord getAttributeSub(long iAttribute) {
		return this.getHibernateTemplate().get(AttributeSubRecord.class,
				iAttribute);
	}

	public void deleteAttributeSub(AttributeSubRecord attributeSub) {
		this.getHibernateTemplate().delete(attributeSub);
	}

	public int deleteAttributeSub(long iAttribute) {
		return this.getHibernateTemplateEx()
				.executeUpdateByHql(
						"delete from t_attribute_sub where i_attribute = ?",
						iAttribute);
	}

}
